//index.js
const pseudoPages = ["abnormalPage", "indexPage", "profilePage"];
const app = getApp();
Page({
    data: {
        newsList: [0, 1, 2, 3, 4, 5, 6].map((index) => {
            return {
                id: index.toString(),
                title: "IMF：垃圾债的繁荣意味着什么？",
                abstract: `在信贷繁荣中，如果垃圾债的份额不断上升，那么接下来三到四年里，经济增长将放缓。`,
                content: `在信贷繁荣中，如果垃圾债的份额不断上升，那么接下来三到四年里，经济增长将放缓。`,
                pictureSource: 'https://wpimg.wallstcn.com/f0548e3c-4d89-4ffc-8f8c-de7afc23fd35.png?imageView2/1/w/720/h/540',
                timestamp: new Date().getTime() + parseInt(24 * 60 * 60 * 1000 * (Math.random() * 2 - 1)),
                // needToReload: false
            };
        }),
        showNewsIndex: null,
        userInitialized: false,
        subNaviBarItems: [{
            name: "异动",
            iconSrc: "/png/abnormal.png",
            activeIconSrc: "/png/abnormal-active.png"
        },{
            name: "首页",
            iconSrc: "/png/index.png",
            activeIconSrc: "/png/index-active.png"
        },{
            name: "用户",
            iconSrc: "/png/profile.png",
            activeIconSrc: "/png/profile-active.png"
        }],

        activePseudoPageIndex: null,
    },
    onSubNaviBarItemTapped: function (e) {
        this.setData({
            activePseudoPageIndex: e.detail
        });
    },
    onNewsChanged: function (e) {
        // console.log(e);
        const oldNewsId = e.target.dataset.newsId;
        const newsList = this.data.newsList;
        const oldNewsIndex = newsList.findIndex(item => item.id === oldNewsId);
        newsList[oldNewsIndex] = e.detail.news;
        this.setData({
            newsList: newsList
        });
    },
    onNewsTapped: function (e) {
        console.log(e);
        const newsId = e.target.dataset.newsId;
        const newsList = this.data.newsList;
        const newsIndex = newsList.findIndex(item => item.id === newsId);
        this.setData({
            showNewsIndex: newsIndex
        });
    },
    onModalCloseButtonTapped: function (e) {
        this.setData({
            showNewsIndex: null
        });
    },
    onLoad: function () {
        const that = this;
        app.initUserInfo().then(() => {
            that.setData({
                userInitialized: true,
                activePseudoPageIndex: 1
            })
        });
        // var that = this;
        // //调用应用实例的方法获取全局数据
        // app.getUserInfo(function (userInfo) {
        //   //更新数据
        //   that.setData({
        //     userInfo: userInfo
        //   })
        //   that.update()
        // })
    }
})